package com.sdhs.examinemanage.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sdhs.common.utils.DateUtils;
import com.sdhs.common.utils.SecurityUtils;
import com.sdhs.examinemanage.domain.GcExamineSupplier;
import com.sdhs.examinemanage.domain.GcExamineSupplierScore;
import com.sdhs.examinemanage.mapper.GcExamineSupplierMapper;
import com.sdhs.examinemanage.service.IGcExamineSupplierService;

/**
 * 供应商考核Service业务层处理
 * 
 * @author wanglei
 * @date 2025-04-01
 */
@Service
public class GcExamineSupplierServiceImpl implements IGcExamineSupplierService 
{
    @Autowired
    private GcExamineSupplierMapper gcExamineSupplierMapper;

    /**
     * 查询供应商考核
     * 
     * @param id 供应商考核主键
     * @return 供应商考核
     */
    @Override
    public GcExamineSupplier selectGcExamineSupplierById(Long id)
    {
    	GcExamineSupplier examineSupplier = gcExamineSupplierMapper.selectGcExamineSupplierById(id);
    	// 得分
    	List<GcExamineSupplierScore> scoreList = gcExamineSupplierMapper.selectScoreList(id);
    	examineSupplier.setScoreList(scoreList);
    	return examineSupplier;
    }

    /**
     * 查询供应商考核列表
     * 
     * @param gcExamineSupplier 供应商考核
     * @return 供应商考核
     */
    @Override
    public List<GcExamineSupplier> selectGcExamineSupplierList(GcExamineSupplier gcExamineSupplier)
    {
        return gcExamineSupplierMapper.selectGcExamineSupplierList(gcExamineSupplier);
    }

    /**
     * 新增供应商考核
     * 
     * @param gcExamineSupplier 供应商考核
     * @return 结果
     */
    @Override
    public int insertGcExamineSupplier(GcExamineSupplier gcExamineSupplier)
    {
    	setScore(gcExamineSupplier);
		gcExamineSupplier.setCreater(SecurityUtils.getUsername());
        gcExamineSupplier.setCreateTime(DateUtils.getNowDate());
        int result = gcExamineSupplierMapper.insertGcExamineSupplier(gcExamineSupplier);
        gcExamineSupplierMapper.insertScoreBatch(gcExamineSupplier);
        return result;
    }
    
    /**
     * 工程类供应商计算总得分
     * @param gcExamineSupplier
     */
    public void setScore(GcExamineSupplier gcExamineSupplier) {
    	if ("0".equals(gcExamineSupplier.getSupplierType())) {
    		List<GcExamineSupplierScore> scoreList = gcExamineSupplier.getScoreList();
    		// 否决指标，score为0表示未勾选，为1表示勾选
    		List<GcExamineSupplierScore> rejectList = scoreList.subList(39, 51);
    		boolean reject = 
    				rejectList.stream().map(GcExamineSupplierScore::getScore).filter(Objects::nonNull).anyMatch(score -> score.compareTo(BigDecimal.ONE) == 0);
    		if (reject) {
    			gcExamineSupplier.setTotalScore(BigDecimal.ZERO);
    		} else {
    			// 计算总得分列表
        		List<GcExamineSupplierScore> computeList = scoreList.subList(0, 39);
        		BigDecimal totalScore = 
        				computeList.stream().map(GcExamineSupplierScore::getScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        		gcExamineSupplier.setTotalScore(totalScore);
    		}
    	}
    }

    /**
     * 修改供应商考核
     * 
     * @param gcExamineSupplier 供应商考核
     * @return 结果
     */
    @Override
    public int updateGcExamineSupplier(GcExamineSupplier gcExamineSupplier)
    {
    	setScore(gcExamineSupplier);
		gcExamineSupplier.setUpdater(SecurityUtils.getUsername());
        gcExamineSupplier.setUpdateTime(DateUtils.getNowDate());
        int result = gcExamineSupplierMapper.updateGcExamineSupplier(gcExamineSupplier);
        gcExamineSupplierMapper.deleteScore(gcExamineSupplier.getId());
        gcExamineSupplierMapper.insertScoreBatch(gcExamineSupplier);
        return result;
    }

    /**
     * 批量删除供应商考核
     * 
     * @param ids 需要删除的供应商考核主键
     * @return 结果
     */
    @Override
    public int deleteGcExamineSupplierByIds(Long[] ids)
    {
        return gcExamineSupplierMapper.deleteGcExamineSupplierByIds(ids);
    }

    /**
     * 删除供应商考核信息
     * 
     * @param id 供应商考核主键
     * @return 结果
     */
    @Override
    public int deleteGcExamineSupplierById(Long id)
    {
        return gcExamineSupplierMapper.deleteGcExamineSupplierById(id);
    }
}
